using SmartCommunity.Services.Community;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.Controllers.Community
{
    /// <summary>
    /// API接口：社区
    /// </summary>
    [ApiController]
    [Route("api/community/[controller]")]
    public class CommunityController : BaseController
    {
        #region 构造函数
        private readonly CommunityService communityService;
        public CommunityController(CommunityService communityService)
        {
            this.communityService = communityService;
        }
        #endregion

        #region 查询导出 
        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpGet]
        public async Task<PageResult<CommunityDto>> GetList([FromQuery] CommunityQuery query, [FromQuery] Pager pager)
        {
            return await communityService.FindPagedList(query, pager, CurrentUser);
        }

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<CommunityDto> GetOne(Guid id)
        {
            return await communityService.FindCommunityById(id);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        [HttpGet("export")]
        public async Task<ActionResult> ExportExcel([FromQuery] CommunityQuery query)
        {
            var pageData = await communityService.FindPagedList(query, Pager.Unpaged, CurrentUser);
            var exporter = new ExcelExporter();
            exporter.AddTitleRow(0, "社区名称", "负责人姓名", "负责人电话", "成立日期 ", "社区简介");
            int rowIndex = 1;
            foreach (var data in pageData.DataList)
            {
                exporter.AddDataRow(rowIndex++, 0, data.CommunityName, data.ManagerName, data.ManagerTel, data.CreatedDate, data.Remark);
            }
            var bytes = exporter.GetExcelData();
            return File(bytes, CONTENT_TYPE_EXCEL, $"社区导出{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增社区
        /// </summary>
        [HttpPost]
        [Log("新增社区", OperateType.Create, false, true)]
        public async Task<ActionResult<CommunityDto>> Create([FromBody] CommunityDto dto)
        {
            var community = await communityService.CreateCommunity(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = community.CommunityId }, community);
        }

        /// <summary>
        /// 修改社区
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改社区", OperateType.Update)]
        public async Task<ActionResult> Update(Guid id, [FromBody] CommunityDto dto)
        {
            if (id != dto.CommunityId)
            {
                return ErrorMessage("要更新的数据和ID不匹配");
            }
            await communityService.UpdateCommunity(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除社区
        /// </summary>
        [HttpDelete]
        [Log("删除社区", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] Guid[] ids)
        {
            if (ids.Length == 0)
            {
                return ErrorMessage("参数错误：没有接收到要删除的主键值");
            }
            await communityService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
